package levelSelect;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import networking.SMap;


@SuppressWarnings("serial")
public class Icon extends JPanel
{
	private int[][] m;
	private int width;
	private int height;
	private boolean selected = false;
	
	public Icon(String file)
	{
		File inputFile = new File(file);
		BufferedImage b;
		try
		{
			b = ImageIO.read(inputFile);
			height = b.getHeight();
			width = b.getWidth();
			m = new int[width][height];
			for(int i=0; i<width; i++)
			{
				for(int j=0; j<height; j++)
				{
					m[i][j] = b.getRGB(j, i);
				}
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			System.exit(0);
		}
		
	}
	
	public Icon(URL file)
	{
		URL inputFile = file;
		BufferedImage b;
		try
		{
			b = ImageIO.read(inputFile);
			height = b.getHeight();
			width = b.getWidth();
			m = new int[width][height];
			for(int i=0; i<width; i++)
			{
				for(int j=0; j<height; j++)
				{
					m[i][j] = b.getRGB(j, i);
				}
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			System.exit(0);
		}
		
	}
	
	public Icon(SMap sMap) 
	{
		int[][] k = sMap.getMap();
		m = new int[k.length][k[0].length];
		for(int i=0; i<k[0].length; i++)
		{
			for(int j=0; j<k.length; j++)
			{
				m[i][j] = k[j][i];
			}
		}
		width = sMap.getWidth();
		height = sMap.getHeight();
	}

	public void paintComponent(Graphics g)
	{
		if(selected)
		{
			g.setColor(Color.blue);
		}
		else
		{
			g.setColor(Color.green);
		}
		g.fillRect(0,0,getWidth(),getHeight());
		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				Color temp = new Color(m[i][j]);
				Color temp2 = new Color(temp.getRed() / 2, temp.getGreen() / 2, temp.getBlue() / 2);
				if(selected)
				{
					g.setColor(temp);
				}
				else
				{
					g.setColor(temp2);
				}
				g.fillRect(j*3, i*3, 3, 3);
			}
		}
	}

	public void setSelected(boolean b) 
	{
		selected = b;
		
	}

	public boolean isSelected() 
	{
		return selected;
	}

	public SMap getMap() 
	{
		return new SMap(m, "");
	}
}